﻿using System.Net;
using System.Windows;
using System.Windows.Controls;
using System;
using System.Windows.Browser;

namespace SamplesShow
{
    //这是一个例子展示程序
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in SamplesCollection.Current.Keys)
            {
                TreeViewItem root = new TreeViewItem();
                root.Header = item;
                foreach (var t in SamplesCollection.Current[item])
                    root.Items.Add(t);
                treeViewSamples.Items.Add(root);
                root.IsExpanded = true;
            }
            treeViewSamples.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(treeViewSamples_SelectedItemChanged);
            SamplesCollection.Current.BeginInitialize();
            SamplesCollection.Current.DownloadProgressChanged += new DownloadProgressChangedEventHandler(SamplesCollection_DownloadProgressChanged);
            SamplesCollection.Current.OpenReadCompleted += new OpenReadCompletedEventHandler(SamplesCollection_OpenReadCompleted);
            laoding.Visibility = System.Windows.Visibility.Visible;
        }

        void SamplesCollection_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            laoding.Visibility = System.Windows.Visibility.Collapsed;
            SamplesCollection.Current.DownloadProgressChanged -= SamplesCollection_DownloadProgressChanged;
            SamplesCollection.Current.OpenReadCompleted -= SamplesCollection_OpenReadCompleted;
        }

        void SamplesCollection_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressValue.Text = e.ProgressPercentage + "%";
            progressName.Text = e.UserState.ToString();
        }
        TreeItem_Sample showTreeItem_Sample = null;
        void treeViewSamples_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is TreeItem_Sample)
            {
                if (showTreeItem_Sample != null)
                    showTreeItem_Sample.RemoveSampleUIElement(sampleview);
                var ui = (e.NewValue as TreeItem_Sample).GetUIElement();
                if (sampleview.Children.Contains(ui))
                    return;
                if ((e.NewValue as TreeItem_Sample).BlankUri == null)
                {
                    sampleview.Children.Add(ui);
                    textIllustrate.Text = (e.NewValue as TreeItem_Sample).Illustrate;


                    showTreeItem_Sample = e.NewValue as TreeItem_Sample;
                }
                else
                {
                    HtmlPage.Window.Navigate((e.NewValue as TreeItem_Sample).BlankUri, "_blank");
                }
            }
        }
    }
}
